package com.adou.d3.example;

import com.adou.d3.pojo.Brand;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.junit.Test;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * 品牌类的增删改查功能
 */
public class BrandTest {

    /**
     * 查询所有
     * 1.select * from tb_brand
     * 2.参数：不需要
     * 3.结果：List<Brand>
     *
     */
    @Test
    public void testSelectAll() throws Exception {
        System.out.println(System.getProperty("user.dir"));
        // 1.获取连接conn对象
        Properties prop = new Properties();
        prop.load(new FileInputStream("src/druid.properties"));

        // 4.获取连接对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);

        // 5.获取数据库连接
        Connection connection = dataSource.getConnection();

        // 2.定义sql语句
        String sql = "select * from tb_brand";

        // 3.获取pstmt对象
        PreparedStatement pstmt = connection.prepareStatement(sql);

        // 4.设置参数

        // 5.执行sql
        ResultSet rs = pstmt.executeQuery();

        // 6.处理结果List<Brand>
        List<Brand> brands = new ArrayList<>();
        Brand brand = null;
        while (rs.next()) {
            // 获取数据
            int id = rs.getInt("id");
            String brandName = rs.getString("brand_name");
            String companyName = rs.getString("company_name");
            int ordered = rs.getInt("ordered");
            String description = rs.getString("description");
            int status = rs.getInt("status");

            // 封装对象
            brand = new Brand();
            brand.setId(id);
            brand.setBrandName(brandName);
            brand.setCompanyName(companyName);
            brand.setOrdered(ordered);
            brand.setDescription(description);
            brand.setStatus(status);

            // 装载集合
            brands.add(brand);
        }

        System.out.println(brands);
        // 7.释放资源
        rs.close();
        pstmt.close();
        connection.close();

    }


    /**
     * 添加
     * sql:insert into tb_brand (brand_name, company_name, ordered, description, status) vaules()
     * 参数：需要，除id之外的所有信息
     * 结果：boolean
     * @throws Exception
     */
    @Test
    public void testAdd() throws Exception {
        // 接收页面传递参数
        String brandName ="香飘飘";
        String companyName = "香飘飘旗舰店";
        int ordered = 1;
        String description = "一年卖出绕地球7圈";
        int status = 1;


        // 1.获取连接conn对象
        Properties prop = new Properties();
        prop.load(new FileInputStream("src/druid.properties"));

        // 4.获取连接对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);

        // 5.获取数据库连接
        Connection connection = dataSource.getConnection();

        // 2.定义sql语句
        String sql = "insert into tb_brand (brand_name, company_name, ordered, description, status) values(?,?,?,?,?)";

        // 3.获取预编译对象
        PreparedStatement pstmt = connection.prepareStatement(sql);
        // 4.设置参数
        pstmt.setString(1, brandName);
        pstmt.setString(2, companyName);
        pstmt.setInt(3, ordered);
        pstmt.setString(4, description);
        pstmt.setInt(5, status);

        // 5.执行结果
        int count = pstmt.executeUpdate();
        if(count > 0) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }


    }


    /**
     * 修改
     * sql:update tb_brand set brand_name=?, company_name=?, ordered=?, description=?, status=? where id=?
     * 参数：需要，除id之外的所有信息
     * 结果：boolean
     * @throws Exception
     */
    @Test
    public void testUpd() throws Exception {
        // 接收页面传递参数
        String brandName ="香飘飘1";
        String companyName = "香飘飘旗舰店1";
        int ordered = 1;
        String description = "一年卖出绕地球7圈1";
        int status = 1;
        int id = 4;


        // 1.获取连接conn对象
        Properties prop = new Properties();
        prop.load(new FileInputStream("src/druid.properties"));

        // 4.获取连接对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);

        // 5.获取数据库连接
        Connection connection = dataSource.getConnection();

        // 2.定义sql语句
        String sql = "update tb_brand set brand_name=?, company_name=?, ordered=?, description=?, status=? where id=?";

        // 3.获取预编译对象
        PreparedStatement pstmt = connection.prepareStatement(sql);
        // 4.设置参数
        pstmt.setString(1, brandName);
        pstmt.setString(2, companyName);
        pstmt.setInt(3, ordered);
        pstmt.setString(4, description);
        pstmt.setInt(5, status);
        pstmt.setInt(6, id);

        // 5.执行结果
        int count = pstmt.executeUpdate();
        if(count > 0) {
            System.out.println("修改成功");
        } else {
            System.out.println("修改失败");
        }


    }

}
